package com.yzr.controller;

import com.sun.star.ucb.MissingInputStreamException;
import com.yzr.service.DocumentService;
import com.yzr.util.FileType;
import com.yzr.util.converter.Conversion;
import com.yzr.util.converter.ConversionFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jodconverter.core.office.OfficeException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @description
 * @author: yzr
 * @date: 2021/10/26 14:18
 **/
@RestController
@RequestMapping("/document/")
@Api(value = "文件预览", tags = "文件预览")
public class TestController {


    private final DocumentService documentService;
    private final ConversionFactory conversionFactory;

    public TestController(DocumentService documentService, ConversionFactory conversionFactory) {
        this.documentService = documentService;
        this.conversionFactory = conversionFactory;
    }

    /**
     * 转换接口
     *
     * @param from 从
     * @param to   去
     */
    @GetMapping("change")
    @ApiOperation(value = "转换接口", tags = "转换接口", httpMethod = "GET")
    public void conversion(@RequestParam("from") String from,
                           @RequestParam("to") String to) throws OfficeException {
        documentService.conversion(from, to);

    }

    /**
     * 转换接口
     *
     * @param from 从
     * @param to   去
     */
    @GetMapping("view")
    @ApiOperation(value = "文件预览接口", tags = "文件预览接口", httpMethod = "GET")
    public void fileView(@RequestParam("from") String from,
                         @RequestParam("to") String to, HttpServletResponse response) throws OfficeException, FileNotFoundException {
        documentService.view(from, to, response);

    }

    /**
     * 转换接口
     *
     * @param from 从
     * @param to   去
     */
    @GetMapping("image")
    @ApiOperation(value = "图片压缩接口", tags = "图片压缩接口", httpMethod = "GET")
    public void image(@RequestParam("from") String from,
                      @RequestParam("to") String to, HttpServletResponse response) throws OfficeException, IOException {
        Conversion conversion = conversionFactory.getInstance(FileType.Support.TEXT);
        conversion.convert(from, to);

    }

    /**
     * 转换接口
     *
     * @param from 从
     * @param to   去
     */
    @GetMapping("text")
    @ApiOperation(value = "文本转html接口", tags = "文本转html接口", httpMethod = "GET")
    public void html(@RequestParam("from") String from,
                     @RequestParam("to") String to, HttpServletResponse response) throws MissingInputStreamException, IOException {
        response.setCharacterEncoding("utf-8");
        Conversion conversion = conversionFactory.getInstance(FileType.Support.TEXT);
        conversion.convert(new FileInputStream(from), response.getOutputStream());
    }
}
